const axios = require('axios')
module.exports = {
  devServer: {
    before (app) {
      app.get('/api/getTopBanner', (req, res) => {
        const url = 'https://u.y.qq.com/cgi-bin/musicu.fcg'
        const jumpPrefix = 'https://y.qq.com/n/yqq/album/'
        axios.get(url, {
          headers: {
            referer: 'https://y.qq.com/?ADTAG=myqq',
            origin: 'https://y.qq.com'
          },
          params: req.query
        }).then(response => {
          response = response.data
          if (response.code === 0) {
            const sliderList = []
            const content = response.focus.data && response.focus.data.content

            for (let i = 0; i < content.length; i++) {
              const sliderItem = {
                id: content[i].id,
                linkUrl: jumpPrefix + content[i].jump_info.url + '.html',
                picUrl: content[i].pic_info.url
              }
              sliderList.push(sliderItem)
            }
            res.json({
              code: 0,
              data: {
                sliderList
              }
            })
          } else {
            res.json(response)
          }
        }).catch(err => {
          console.log(err)
        })
      })
      app.get('/api/getHotRecommend', (req, res) => {
        const url = 'https://u.y.qq.com/cgi-bin/musicu.fcg'
        axios.get(url, {
          headers: {
            referer: 'https://y.qq.com/?ADTAG=myqq',
            origin: 'https://y.qq.com'
        },
          params: req.query
      }).then(response => {
        if (response.data.code === 0) {
          res.json(response.data)
        } else {
          res.json(response)
        }
      }) .catch(err => {
        console.log(err)
      })
    })
    app.get('/api/getSingerList', (req, res) => {
        const url = 'https://c.y.qq.com/v8/fcg-bin/v8.fcg'
        axios.get(url, {
          headers: {
            referer: 'https://y.qq.com/portal/playlist.html',
            origin: 'https://y.qq.com'
          },
          params: req.query
        }).then(response => {
          res.json(response.data)
        }).catch(err => {
          console.log(err)
        })
      })
    app.get('/api/getSingerDetail', (req, res) => {
      const url = 'https://c.y.qq.com/v8/fcg-bin/fcg_v8_singer_track_cp.fcg'
      axios.get(url, {
        // headers: {
        //   referer: 'https://y.qq.com/portal/playlist.html',
        //   origin: 'https://y.qq.com'
        // },
        params: req.query
      }).then(response => {
        res.json(response.data)
      }).catch(err => {
        console.log(err)
      })
    })
    }
  }
}

